package com.sample.wanandroidx.basic

import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.view.WindowManager
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDelegate
import androidx.viewbinding.ViewBinding
import com.sample.wanandroidx.app.App
import com.sample.wanandroidx.ext.ActivityViewBindingInflater
import com.sample.wanandroidx.ext.OnNetworkChangeEvent
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

abstract class BaseActivity<VB : ViewBinding>(private val bindingInflater: ActivityViewBindingInflater<VB>) :
    AppCompatActivity() {

    private var lastClickTime: Long = 0

    /**
     * 初始化binding
     */

    protected lateinit var binding: VB

    private var mHandler: Handler? = null

    protected fun getHandler(): Handler? = mHandler

    /**
     * 初始化 数据或视图
     */
    abstract fun initialization()

    /**
     * 是否使用 EventBus
     */
    open fun useEventBus(): Boolean = true

    /**
     * 是否需要显示网络提示 默认显示
     */
    open fun enableNetworkTip(): Boolean = true

    /**
     * 在主线程中执行
     */
    open fun post(runnable: () -> Unit) = getHandler()?.post(runnable)

    /**
     * 在主线程中延时执行
     */
    open fun postDelayed(delayMilli: Long, runnable: () -> Unit) = getHandler()?.postDelayed(runnable, delayMilli)

    open fun onRetry() {
        initialization()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN)
        super.onCreate(savedInstanceState)

        mHandler = Handler(Looper.getMainLooper())
        binding = bindingInflater.invoke(layoutInflater)

        setContentView(binding.root)

        if (useEventBus()) {
            EventBus.getDefault().register(this)
        }

        initialization()
    }

    /**
     * Network Change
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    open fun onNetworkChangeEvent(eventOn: OnNetworkChangeEvent) {
        checkNetwork(eventOn.isConnected)
    }

    private fun checkNetwork(isConnected: Boolean) {
        if (enableNetworkTip()) {
            if (isConnected) {
                if (App.instance().getCurrentVisibleActivity() == this) {
                    //ToastUtil.show("网络已恢复")
                }
            } else {
                //ToastUtil.show("网络已断开") //此处http请求失败会提示
            }
        }
    }

    protected fun useNightMode(isNight: Boolean) {
        if (isNight) {
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
        } else {
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
        }
        recreate()
    }

    override fun onDestroy() {
        if (useEventBus()) {
            EventBus.getDefault().unregister(this)
        }
        mHandler?.removeCallbacksAndMessages(null)
        mHandler = null
        super.onDestroy()
    }

    protected fun backPress() {
        if (lastClickTime <= 0) {
            lastClickTime = System.currentTimeMillis()
        } else {
            val currentClickTime = System.currentTimeMillis()
            if (currentClickTime - lastClickTime < 1000) {
                super.onBackPressed()
            } else {
                lastClickTime = currentClickTime
            }
        }
    }

}